package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.common.BaseContext;
import com.itheima.pojo.R;
import com.itheima.pojo.ShoppingCart;
import com.itheima.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;


    @PostMapping("/add")

    public R addShoppingCart(@RequestBody ShoppingCart shoppingCart){

        log.info("添加购物车");
        long userId = BaseContext.getCurrentId();

        shoppingCart.setUserId(userId);

        Long dishId = shoppingCart.getDishId();
        Long setMealId = shoppingCart.getSetmealId();
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,userId);
        String dishFlavor = shoppingCart.getDishFlavor();
        if(dishId!=null){
            lqw.eq(ShoppingCart::getDishId,dishId);
            lqw.eq(dishFlavor!=null,ShoppingCart::getDishFlavor,dishFlavor);

        }else{
            lqw.eq(ShoppingCart::getSetmealId,setMealId);
        }

        ShoppingCart shopCart = shoppingCartService.getOne(lqw);
        if(shopCart==null){
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            shopCart=shoppingCart;
        }else{
            Integer number = shopCart.getNumber();
            shopCart.setNumber(number+1);
            shoppingCartService.updateById(shopCart);
        }

            return R.success(shopCart);

    }

    @GetMapping("list")

    public R list(){
        log.info("查看购物车");
        long userId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,userId);
        List<ShoppingCart> list = shoppingCartService.list(lqw);
        return R.success(list);
    }

    @DeleteMapping("/clean")
    public R clean(){
        log.info("清空购物车");
        long userId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,userId);
        shoppingCartService.remove(lqw);
        return R.success("购物车清空成功!");
    }

    @PostMapping("/sub")
    public R subtract(@RequestBody ShoppingCart shoppingCart){
        log.info("删除购物车");

        long userId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,userId);
        Long dishId = shoppingCart.getDishId();
        String dishFlavor = shoppingCart.getDishFlavor();
        //是有口味的菜品
        if(dishId!=null&&dishFlavor!=null){
            lqw.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
            lqw.eq(ShoppingCart::getDishFlavor,dishFlavor);
        }else if(dishId!=null){
            lqw.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        }else{
            lqw.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }



        ShoppingCart shopCData = shoppingCartService.getOne(lqw);
        Integer number = shopCData.getNumber();
        shopCData.setNumber(number-1);
        if (number>1){
            shoppingCartService.update(shopCData,lqw);
        }else{
            shoppingCartService.removeById(shopCData);
        }
         return R.success(shopCData);

    }

}